package com.mtgg.ctrl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.mtgg.common.response.ServerResponse;
import com.mtgg.dto.UserDto;
import com.mtgg.req.UserReq;
import com.mtgg.service.TestService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Properties;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Api(tags = "测试")
@RequestMapping("/test")
@RestController
public class TestCtrl {
    //推荐创建不可变静态类成员变量
    private static final Log log = LogFactory.get();

    @Autowired
    private TestService testService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${key1}")
    private String key1;


    /**
     * @Author: 亦善
     * @Date: 2020/5/15 10:38
     * @Describe:
     */
    public static void main(String[] args) throws InterruptedException {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
        ExecutorService executorService = new ThreadPoolExecutor(100,200,200L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),namedThreadFactory);
        ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<String,AtomicInteger>();
        AtomicInteger key = new AtomicInteger(1);
        map.put("key", key);
        //100个线程，对key进行+1 1000次
        for (int i = 0; i < 1000; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    map.get("key").incrementAndGet(); //step 1
                }
            });
        }
        Thread.sleep(3000); //模拟等待执行结束
        System.out.println("------" + map.get("key") + "------");
        executorService.shutdown();
    }

    @GetMapping("/apollo/get")
    public Object apolloget(){
        return key1;
    }

    /**
     * @Author: 亦善
     * @Date: 2020/5/15 17:57
     * @Describe: redis do
     */
    @GetMapping("/redis/info")
    public ServerResponse info(){
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        // 获取redis内存信息
        Properties infoMemory = redisConnection.info("memory");
        log.info("infoMemory:{}", JSON.toJSONString(infoMemory));
        Properties info = redisConnection.info();
        log.info("info:{}", JSON.toJSONString(info));

        Long size = redisConnection.dbSize();
        log.info("size:{}", size);


        redisConnection.close();
        return ServerResponse.createBySuccess(JSON.toJSONString(info));
    }

    @GetMapping("/redis/set")
    public ServerResponse set(String key,String value){
        redisTemplate.opsForValue().set(key,value, 60*60, TimeUnit.SECONDS);
        return ServerResponse.createBySuccess(redisTemplate.opsForValue().get(key));
    }
    @GetMapping("/redis/get")
    public ServerResponse get(String key){

        return ServerResponse.createBySuccess(redisTemplate.opsForValue().get(key));
    }

    @GetMapping("/page")
    public ServerResponse page(UserReq userReq, Integer currentPage, Integer pageSize){
        return testService.getPage(userReq, currentPage,pageSize);
    }

    @ApiOperation(value = "获取用户列表", notes = "用户列表")
    @GetMapping("/a")
    public Object a(Integer a) {
        log.info("6666666");
        log.debug("777777777");
        UserDto userDto = new UserDto();
        userDto.setName(a.toString());
        return "1111111111";
    }

    @GetMapping("/b")
    public ServerResponse b(UserReq userReq){
        return testService.getByCondition(userReq);
    }

    @ApiOperation(value = "post用户", notes = "解释")
    @ApiImplicitParam(paramType = "body", dataType = "UserDto", name = "userDto", value = "用户dto", required = true)
    @PostMapping("/userd")
    public Object userd(@ApiParam("userDto") @RequestBody UserDto userDto) {
        System.out.println("1111111");
        userDto.setName("");
        return "1111111111";
    }

    @ApiOperation(value = "获取用户列表2", notes = "用户列表")
    @GetMapping("/user")
    public Object user() {
        CompletableFuture.runAsync(()->{
            log.info("77777777777777777777777777");
        });
        return testService.getUsers();
    }

}
